package com.neuedu.corejava.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 数据库工具类
 * 
 * @author admit
 *
 */
public class DBHelper {

	// 4个参数
	/**
	 * 1、驱动
	 */
	private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
	/**
	 * 2、url
	 */
	private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
	/**
	 *  3、账号
	 */
	private static final String USER = "jdbcuser";
	/**
	 * 4、密码
	 */
	private static final String PASSWORD = "123456";

	public static Connection openConnection() {
		Connection conn = null;
		try {
			// 1、注册驱动
			Class.forName(DRIVER);
			// 2、连接数据库
			conn = DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void closeAll(ResultSet rs, Statement stmt, Connection conn) {
		// 这个一个标准的关闭动作
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
			}
		}
	}
}
